package org.wangpf.util;

import java.util.ArrayList;
import java.util.List;


public class ArrayUtils {
	/**
	 * 将数组转换为列表
	 * 
	 * @param items
	 * @return
	 */
	public static List<?> arrayToList(Object[] items) {

		List<Object> list = new ArrayList<Object>();
		for (int i = 0; i < items.length; i++)
			list.add(items[i]);

		return list;
	}

	/**
	 * 将数组转为特定格式的字符串
	 * 
	 * Example: values = new String[] { '1', '2', '3' };<br>
	 * System.out.println('delete from User where code in (' +
	 * Utils.arrayToSting(values, ',', true) + ')');<br>
	 * System.out.println('delete from User where code in (' +
	 * Utils.arrayToSting(values, ',', false) + ')');
	 * 
	 * @param values
	 *            字符数组
	 * @param split
	 *            分隔符
	 * @param asNumber
	 *            作为数字分隔
	 * @return
	 */
	public static String arrayToSting(String[] values, String split,
			boolean asNumber) {
		StringBuffer buf = new StringBuffer("");
		String fix = "'";
		if (asNumber)
			fix = "";
		if (values != null)
			for (int i = 0; i < values.length; i++)
				if (i == 0)
					buf.append(fix + values[i] + fix);
				else
					buf.append(split + fix + values[i] + fix);
		return buf.toString();
	}

	/**
	 * 数组转为字符串
	 * 
	 * @param items
	 * @param splitChar
	 * @return
	 */
	public static String arrayToString(int[] items, String splitChar) {
		StringBuffer buf = new StringBuffer("");
		if (items != null)
			for (int i = 0; i < items.length; i++)
				if (i > 0)
					buf.append(splitChar + items[i]);
				else
					buf.append(items[i]);
		return buf.toString();
	}

	/**
	 * 数组转为字符串
	 * 
	 * @param items
	 * @param splitChar
	 * @return
	 */
	public static String arrayToString(Object[] items, String splitChar) {
		StringBuffer buf = new StringBuffer("");
		if (items != null)
			for (int i = 0; i < items.length; i++)
				if (i > 0)
					buf.append(splitChar + items[i]);
				else
					buf.append(items[i]);
		return buf.toString();
	}

	/**
	 * 转换字符串数组为整型数组
	 */
	public static int[] convert(String[] items) {
		int[] ret = new int[items.length];
		for (int i = 0; i < items.length; i++)
			ret[i] = Integer.parseInt(items[i]);
		return ret;
	}



	/**
	 * 
	 * @param items
	 *            数字型字符数组
	 * @return 整形数组
	 */
	public static int[] strArrayToInt(String[] items) {
		return convert(items);
	}

	/**
	 * 将特定分隔符分隔的字符串转换为列表
	 * 
	 * @param source
	 *            源串
	 * @param splitchar
	 *            分隔符
	 * @return
	 */
	public static List<?> stringToList(String source, String splitchar) {

		if (source == null)
			return new ArrayList<Object>();
		else
			return arrayToList(source.split(splitchar));

	}
}
